import { createStore } from 'vuex'
import router from '../router'

export default createStore({
    state: {
        user: null,
    },
    mutations: {
        setUser(state, user) {
            state.user = user
            router.replace('/home')
        },
    },
    actions: {
        async getUser(store, { http, query }) {
            const {
                data: { data, success },
            } = await http.post('api/login', query)
            if (!success) return
            store.commit('setUser', data)
            const account = encodeURIComponent(JSON.stringify({ account: data.account, password: data.password }))
            window.localStorage.setItem('account', account)
        },
    },
    modules: {},
})
